<template>
  <div class="scalable-pages">
    <main class="main">
      <!-- 产品系列导航 -->
      <div class="product-series">
        <ul class="nav">
          <li class="nav-arrow left-arrow" @click="prevCategory" :class="{ disabled: categoryOffset === 0 }">
            <i class="iconfont icon-arrow-left"></i>
          </li>

          <li v-for="(item, index) in visibleCategories" :key="item.id" :class="{
            active: selectedIndex === index + categoryOffset,
          }" @click.stop="handleNavClick(index)">
            <span>{{ item.name }}</span>
          </li>

          <li class="nav-arrow right-arrow" @click="nextCategory"
            :class="{ disabled: categoryOffset >= navList.length - maxVisibleCategories }">
            <i class="iconfont icon-arrow-right"></i>
          </li>
        </ul>
      </div>

      <!-- 产品详情 -->
      <div class="product-detail" v-if="filmInfo">
        <div class="product-title">
          <div class="left">
            <h2>{{ filmInfo.name }}</h2>
            <span>{{ filmInfo.position }}</span>
          </div>

          <div class="right">
            <div class="item" @click="prevFilm" :class="{ disabled: filmIndex === 0 }">
              <i class="iconfont icon-arrowleft"></i>
            </div>
            <div class="list">
              <div class="item" v-for="(item, index) in visibleFilms" :key="index"
                :class="{ active: index + filmOffset === filmIndex }" @click="filmIndex = index + filmOffset">
                {{ item.name }}
              </div>
            </div>
            <div class="item" @click="nextFilm" :class="{ disabled: filmIndex === filmList.length - 1 }">
              <i class="iconfont icon-arrowright"></i>
            </div>
          </div>
        </div>

        <div class="product-content">
          <div class="flex flex-row">
            <!-- 产品评分 -->
            <div class="product-ratings">
              <div class="rating-item">
                <span>隔热性能:</span>
                <div class="stars">
                  <rate :modelValue="filmInfo.heat_performance"></rate>
                </div>
              </div>
              <div class="rating-item">
                <span>防晒性能:</span>
                <div class="stars">
                  <rate :modelValue="filmInfo.uv_performance"></rate>
                </div>
              </div>
              <div class="rating-item">
                <span>安全指数:</span>
                <div class="stars">
                  <rate :modelValue="filmInfo.safety_index"></rate>
                </div>
              </div>
              <div class="rating-item">
                <span>耐用指数:</span>
                <div class="stars">
                  <rate :modelValue="filmInfo.durability_index"></rate>
                </div>
              </div>
            </div>

            <div class="product-image">
              <img :src="filmInfo.image" :alt="filmInfo.name" v-animate-on-scroll="'slideInRight'" />
            </div>
          </div>

          <div class="flex flex-row">
            <div class="product-features">
              <p v-for="(feature, index) in featuresList" :key="index">
                {{ feature }}
              </p>
            </div>

            <!-- 产品参数 -->
            <div class="product-specs">
              <div class="spec-item">
                <div class="spec-icon">
                  <img src="/static/可见光.png" alt="" />
                </div>
                <div class="spec-content">
                  <div class="spec-value">
                    {{ filmInfo.visible_light_transmittance }}%
                  </div>
                  <div class="spec-label">可见光透射比</div>
                </div>
              </div>
              <div class="spec-item">
                <div class="spec-icon">
                  <img src="/static/太阳能总阻隔率.png" alt="" />
                </div>
                <div class="spec-content">
                  <div class="spec-value">
                    {{ filmInfo.solar_energy_rejection }}%
                  </div>
                  <div class="spec-label">太阳能总阻隔率</div>
                </div>
              </div>
              <div class="spec-item">
                <div class="spec-icon">
                  <img src="/static/可见光反射比.png" alt="" />
                </div>
                <div class="spec-content">
                  <div class="spec-value">
                    {{ filmInfo.visible_light_reflectance }}%
                  </div>
                  <div class="spec-label">可见光反射比</div>
                </div>
              </div>
              <div class="spec-item">
                <div class="spec-icon">
                  <img src="/static/紫外线阻隔率.png" alt="" />
                </div>
                <div class="spec-content">
                  <div class="spec-value">{{ filmInfo.uv_rejection }}%</div>
                  <div class="spec-label">紫外线阻隔率</div>
                </div>
              </div>
              <div class="spec-item">
                <div class="spec-icon">
                  <img src="/static/厚度.png" alt="" />
                </div>
                <div class="spec-content">
                  <div class="spec-value">{{ filmInfo.thickness }}mil</div>
                  <div class="spec-label">厚度</div>
                </div>
              </div>
              <div class="spec-item">
                <div class="spec-icon">
                  <img src="/static/质保.png" alt="" />
                </div>
                <div class="spec-content">
                  <div class="spec-value">{{ filmInfo.warranty }}年</div>
                  <div class="spec-label">质保</div>
                </div>
              </div>
            </div>
          </div>
        </div>
      </div>
      <div class="empty" v-if="!filmInfo">该分类暂无数据</div>
    </main>
  </div>
</template>

<script>
import { get } from "@/utils/request";
import rate from "../components/rate.vue";

export default {
  components: {
    rate,
  },
  data() {
    return {
      selectedIndex: 0,
      navList: [],
      currentNav: {},
      filmList: [],
      filmIndex: 0,
      categoryOffset: 0,
      filmOffset: 0,
      maxVisibleCategories: 5,
      maxVisibleFilms: 4,
    };
  },
  computed: {
    visibleCategories() {
      return this.navList.slice(
        this.categoryOffset,
        this.categoryOffset + this.maxVisibleCategories
      );
    },
    visibleFilms() {
      return this.filmList.slice(
        this.filmOffset,
        this.filmOffset + this.maxVisibleFilms
      );
    },
    filmInfo() {
      return this.filmList[this.filmIndex];
    },
    featuresList() {
      if (!this.filmInfo || !this.filmInfo.description) return [];
      // 使用正则表达式分割字符串，支持中文逗号、顿号和句号
      return this.filmInfo.description
        .split(/[，。,]/)
        .filter((item) => item.trim() !== "");
    },
  },
  methods: {
    handleNavClick(index) {
      this.selectedIndex = index + this.categoryOffset;
      this.currentNav = this.navList[this.selectedIndex];
      this.getFilmList();
      this.filmIndex = 0;
      this.filmOffset = 0;
    },
    prevCategory() {
      if (this.categoryOffset > 0) {
        this.categoryOffset--;
      }
    },
    nextCategory() {
      if (
        this.categoryOffset <
        this.navList.length - this.maxVisibleCategories
      ) {
        this.categoryOffset++;
      }
    },
    prevFilm() {
      if (this.filmIndex > 0) {
        this.filmIndex--;
        if (this.filmIndex < this.filmOffset) {
          this.filmOffset--;
        }
      }
    },
    nextFilm() {
      if (this.filmIndex < this.filmList.length - 1) {
        this.filmIndex++;
        if (this.filmIndex >= this.filmOffset + this.maxVisibleFilms) {
          this.filmOffset++;
        }
      }
    },
    getNavList() {
      get("/filmCategory/list", { type: 'window' }).then((res) => {
        if (res.code == 200) {
          this.navList = res?.data || [];
          if (this.navList.length > 0) {
            this.currentNav = this.navList[this.selectedIndex];
            this.getFilmList();
          }
        }
      });
    },
    getFilmList() {
      get("/windowFilm/list", {
        pageSize: 100,
        category_id: this.currentNav.id,
      }).then((res) => {
        if (res.code == 200) {
          this.filmList = res?.data?.list || [];
          this.filmIndex = 0;
          this.filmOffset = 0;
        }
      });
    },
  },
  mounted() {
    this.getNavList();
  },
};
</script>

<style lang="less" scoped>
/* 只修改箭头部分样式，其他保持原样 */
.product-series {
  margin: 0 auto 40px;
  position: relative;
  padding-bottom: 50px;

  .nav {
    display: flex;
    justify-content: center;
    align-items: center;
    list-style: none;
    padding: 0;
    margin: 0;
    position: relative;

    >li {
      padding: 0.3vw 1.6vw;
      margin: 0 1.2vw;
      cursor: pointer;
      color: #333;
      position: relative;
      border: 1px solid #c5000a;
      color: #c5000a;
      transform: skew(-15deg);
      transition: all 0.3s ease;
      background-color: #fff;

      span {
        display: inline-block;
        transform: skew(15deg);
        font-size: 32px;
        font-weight: normal;
        white-space: nowrap;
      }

      &.active {
        color: #fff;
        background-color: #c5000a;
      }

      &:hover:not(.nav-arrow) {
        color: #fff;
        background-color: #c5000a;
      }

      /* 导航箭头样式 */
      &.nav-arrow {
        width: auto;
        /* 移除固定宽度 */
        height: auto;
        /* 移除固定高度 */
        padding: 10px 16px;
        /* 与其他导航项保持一致 */
        margin: 0 1.2vw;
        /* 与其他导航项保持一致 */
        color: #c5000a;
        border: 1px solid #c5000a;
        /* 添加红色边框 */
        transform: skew(-15deg);
        /* 添加倾斜效果 */
        background-color: #fff;
        /* 白色背景 */

        i {
          font-size: 32px;
          transform: skew(15deg);
          /* 反向倾斜图标 */
          display: inline-block;
        }

        &:hover:not(.disabled) {
          color: #fff;
          background-color: #c5000a;
          transform: skew(-15deg);
          /* 保持倾斜效果 */
        }

        &.disabled {
          cursor: not-allowed;
          background-color: #fff;
          color: #ccc;
          border-color: #ccc;

          &:hover {
            transform: skew(-15deg) !important;
            /* 保持倾斜效果 */
            background-color: #fff !important;
            color: #ccc !important;
          }
        }

        &.left-arrow {
          order: -1;
        }

        &.right-arrow {
          order: 1;
        }
      }
    }
  }
}

/* 其他原有样式保持不变 */
.banners {
  width: 100%;
  display: black;
}

.swipers {
  width: 100%;
}

.main {
  background-color: white;
  padding: 100px 0 50px 0;
}

.product-series {
  margin: 0 auto 40px;
  position: relative;
  padding-bottom: 50px;

  ul {
    display: flex;
    justify-content: center;
    list-style: none;
    padding: 0 100px;
    margin: 0;
    position: absolute;
    z-index: 2;
    top: 50%;
    left: 50%;
    transform: translate(-50%, -50%);
  }

  li {
    padding: 0.3vw 1.6vw;
    margin: 0 1.2vw;
    cursor: pointer;
    color: #333;
    position: relative;
    border: 1px solid #c5000a;
    color: #c5000a;
    transform: skew(-15deg);
    transition: all 0.3s ease;
    background-color: #fff;

    span {
      display: inline-block;
      transform: skew(15deg);
      font-size: 32px;
      font-weight: normal;
      white-space: nowrap;
    }
  }

  li.active {
    color: #fff;
    background-color: #c5000a;
    transform: skew(-15deg);
  }

  li:hover {
    color: #fff;
    font-weight: bold;
    background-color: #c5000a;
    transform: skew(-15deg);
  }
}

.product-detail {
  max-width: 1600px;
  margin: 0 auto;
  padding: 0 20px;
  margin-top: 80px;

  * {
    box-sizing: border-box;
  }
}

.product-title {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin: 30px 0;
  position: relative;

  .left {
    display: flex;
    flex-direction: row;
    align-self: start;

    h2 {
      font-size: 68px;
      margin-right: 15px;
      font-weight: bold;
      color: #000;
      padding: 0;
      margin: 0;
    }

    span {
      margin-left: 12px;
      margin-top: 10px;
      color: #000;
      font-size: 30px;
    }
  }

  .right {
    font-size: 32px;
    display: flex;
    align-items: center;
    justify-content: center;

    .item {
      padding: 10px 15px;
      border: 1px solid transparent;
      background: #f5f5f5;
      cursor: pointer;

      &.active {
        border-color: #aeacad;
        background: #fff;
      }

      &.disabled {
        opacity: 0.3;
      }
    }

    .list {
      display: flex;
      gap: 20px;
      margin: 0 15px;
    }
  }
}

.product-ratings {
  display: flex;
  flex-direction: column;
  width: 40%;
  box-sizing: border-box;
  font-size: 32px;

  .rating-item {
    display: flex;
    align-items: center;
    margin-bottom: 30px;
    color: rgb(11, 11, 11);

    .stars {
      margin-right: 30px;
      margin-left: 20px;
    }
  }
}

.product-image {
  display: flex;
  flex: 1;
  padding-left: 4vw;

  img {
    width: 100%;
    height: auto;
  }
}

.product-content {
  display: flex;
  flex-direction: column;
  box-sizing: border-box;

  .flex-row {
    display: flex;
    flex-direction: row;
    margin-bottom: 4vw;
    align-items: center;
  }
}

.product-features {
  width: 40%;
  padding: 2vw 3vw;
  background-color: #f1f1f1;
  font-size: 24px;

  p {
    color: #000;
    line-height: 1.2;
  }
}

.product-specs {
  display: flex;
  justify-content: flex-end;
  flex: 1;
  flex-wrap: wrap;
  padding: 1vw 0 0 4vw;
  margin-left: 30px;

  .spec-item {
    width: calc(33.33%);
    box-sizing: border-box;
    display: flex;
    flex-direction: row;
    align-items: center;
    margin: 20px 0;
    justify-content: flex-start;
  }

  .spec-icon {
    width: 60px;
    height: 60px;
    background-color: #f5f5f5;
    border-radius: 50%;
    margin-right: 15px;

    img {
      width: 100%;
    }
  }

  .spec-content {
    display: flex;
    flex-direction: column;
    align-items: flex-start;

    .spec-value {
      font-size: 2.2vw;
      font-weight: bold;
      color: #000;
      margin-bottom: 5px;
    }

    .spec-label {
      color: #666;
      font-size: 1.4vw;
    }
  }
}

@media screen and (min-width: 1200px) {
  .product-features {
    font-size: 28px;
  }

  .product-specs {
    .spec-content {
      .spec-value {
        font-size: 30px;
      }

      .spec-label {
        font-size: 20px;
      }
    }
  }
}

.empty {
  text-align: center;
  font-size: 24px;
  color: #999;
  padding: 50px 0;
}</style>